package com.ldr;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.w3c.dom.*;


import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.File;
import java.io.FileOutputStream;
/**
 * @author：ldr
 * @date：2025/1/14 14:10
 * @description：将xml文件转成excel 创建两个表 区分需要翻译的字符串和不需要翻译的字符串
 */
public class StringsXmlToExcel {

    public static void convertStringsXmlToExcel(String xmlPath, String excelPath) {
        if (xmlPath.isEmpty()) {
            System.out.println("请输入 XML 文件名字");
            return;
        }
        if (excelPath.isEmpty())  {
            System.out.println("请输入 XML 文件名字");
            return;
        }
        try {
            // Load strings.xml
            File xmlFile = new File(xmlPath);
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document document = builder.parse(xmlFile);

            // Create Excel workbook and sheets
            Workbook workbook = new XSSFWorkbook();
            Sheet translatableSheet = workbook.createSheet("Translatable Strings");
            Sheet nonTranslatableSheet = workbook.createSheet("Non-Translatable Strings");

            // Setup headers for both sheets
            createHeaderRow(translatableSheet);
            createHeaderRow(nonTranslatableSheet);

            // Extract string elements from XML
            NodeList stringNodes = document.getElementsByTagName("string");
            int translatableRowIndex = 1;
            int nonTranslatableRowIndex = 1;

            for (int i = 0; i < stringNodes.getLength(); i++) {
                Node node = stringNodes.item(i);
                if (node.getNodeType() == Node.ELEMENT_NODE) {
                    Element element = (Element) node;
                    String name = element.getAttribute("name");
                    String value = element.getTextContent();
                    String translatable = element.getAttribute("translatable");

                    if ("false".equalsIgnoreCase(translatable)) {
                        addRow(workbook,nonTranslatableSheet, nonTranslatableRowIndex++, name, value);
                    } else {
                        addRow(workbook,translatableSheet, translatableRowIndex++, name, value);
                    }
                }
            }

            // Write the workbook to an Excel file
            try (FileOutputStream outputStream = new FileOutputStream(excelPath)) {
                workbook.write(outputStream);
            }
            workbook.close();

            System.out.println("Strings.xml has been successfully exported to " + excelPath);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void createHeaderRow(Sheet sheet) {
        Row headerRow = sheet.createRow(0);
        Cell nameHeader = headerRow.createCell(0);
        nameHeader.setCellValue("Name");

        Cell valueHeader = headerRow.createCell(1);
        valueHeader.setCellValue("Value");
    }

    private static void addRow(Workbook workbook,Sheet sheet, int rowIndex, String name, String value) {
        Row row = sheet.createRow(rowIndex);
        Cell nameCell = row.createCell(0);
        nameCell.setCellValue(name);

        Cell valueCell = row.createCell(1);
        // 设置单元格为文本格式
        valueCell.setCellValue(String.valueOf(value)); // 强制转换为字符串
    }

//    public static void main(String[] args) {
//        String xmlPath = "D:/java_project/study/AndroidExportProject/base-strings.xml"; // Replace with your strings.xml path D:/java_project/study/AndroidExportProject/strings.xml
//        String excelPath = "D:/java_project/study/AndroidExportProject/base-output.xlsx"; // Replace with desired output Excel file path
//        convertStringsXmlToExcel(xmlPath, excelPath);
//    }
}
